<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>12 事件代理</title>
		<style type="text/css">
			* {
				padding: 0;
				margin: 0;
			}

			ul {
				list-style: none;
				overflow: hidden;
				margin-top: 80px;
			}

			ul li {
				float: left;
				width: 100px;
				height: 30px;
				text-align: center;
				line-height: 30px;
				color: #fff;
				background-color: #000;
				margin: 0 10px
			}
		</style>
	</head>
	<body>
		<ul id="box">
			<li>1</li>
			<li>2</li>
			<li>3</li>
			<li>4</li>
			<li>5</li>

		</ul>
		<script type="text/javascript">
			window.onload = function() {
				
				// 常规方法实现
				// 1.获取标签
				/* var lis = document.getElementsByTagName('li');
				for(var i = 0; i < lis.length; i++){
					lis[i].onmouseover = function (){
						this.style.backgroundColor = 'blue';
					}
					lis[i].onmouseout = function (){
						this.style.backgroundColor = 'black';
					}
				} */

				// 事件代理的方式实现 事件实际目标对象来实现
				// 事件代理应用: 事件实际目标对象target和srcElement属性完成
				// 优点: 提高性能以及降低代码的复杂度
				var box = document.getElementById('box');
				box.onmouseover = function(e) {
					e = e || window.event;
					var target = e.target || e.srcElement;
					target.style.backgroundColor = 'blue';
				}
				box.onmouseout = function(e) {
					e = e || window.event;
					var target = e.target || e.srcElement;
					target.style.backgroundColor = 'black';
				}

			}
		</script>
	</body>
</html>
